package com.liankuan.console.repository;

import com.liankuan.console.dto.Strategy;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.List;

@Component
public interface StrategyRepository extends JpaRepository<Strategy, Integer> {
    /**
     * 获取所有的策略列表
     *
     * @return
     */
    List<Strategy> findAll();

    /**
     * 根据策略名获取策略
     *
     * @param name
     * @return
     */
    Strategy findByName(String name);

    Strategy findByParams(String name);

    /**
     * 根据ID获取策略信息
     *
     * @param id
     * @return
     */
    Strategy findById(int id);

    /**
     * 修改策略的启动状态
     *
     * @param id
     * @param state
     */
    @Query(value = "update strategy set state=?2 where id=?1", nativeQuery = true)
    @Modifying
    @Transactional
    void updateState(int id, int state);

    /**
     * 更新策略的基本信息
     *
     * @param id
     * @param name
     * @param jarName
     */
    @Query(value = "update strategy set name=?2 ,jar_name=?3,email=?4,params=?5 where id=?1", nativeQuery = true)
    @Modifying
    @Transactional
    void updateStrategyBasic(int id, String name, String jarName, String email, String params);

    /**
     * 更新策略的配置信息
     *
     * @param config
     */
    @Query(value = "update strategy set config=?1 where id=?2", nativeQuery = true)
    @Modifying
    @Transactional
    void updateStrategyConfig(String config, int id);

}
